<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Formatter for messages for RSS.
 *
 *  History:
 *  2007-07-22  Gelin   Initially created.
 *
 */

require_once('MessageFormatter.php');

/**
 *  Formatter for messages for RSS.
 */
class RssMessageFormatter extends MessageFormatter {

/**
     *  Prepares message to display.
     *  Escapes HTML tags, ...
     *  @param  $message    message to prepare
     *  @return modified message
     */
    function prepareToDisplay($message) {
        if (isset($message['post_time'])) {
            $message['last_time'] = $message['post_time'];
            $message['rss_post_time'] = $this->formatRssTime($message['post_time']);
            $message['post_time'] = $this->formatTime($message['post_time']);
        }
        if (isset($message['name'])) {
            $message['name'] = $this->formatName($message['name']);
        }
        if (isset($message['email'])) {
            $message['email'] = $this->formatEmail($message['email']);
        }
        if (isset($message['url'])) {
            $message['url'] = $this->formatUrl($message['url']);
        }
        if (isset($message['message'])) {
            $message['message'] = $this->formatMessage($message['message']);
        }
        if (isset($message['reply_time'])) {
            $message['last_time'] = $message['reply_time'];
            $message['rss_reply_time'] = $this->formatRssTime($message['reply_time']);
            $message['reply_time'] = $this->formatTime($message['reply_time']);
        }
        if (isset($message['reply'])) {
            $message['reply'] = $this->formatMessage($message['reply']);
        }
        if (isset($message['last_time'])) {
            $message['rss_last_time'] = $this->formatRssTime($message['last_time']);
        }
        return $message;
    }

    /**
     *  Formats time string.
     *  @param  $timestamp timestamp from database
     *  @return string represetation of time
     */
    function formatRssTime($timestamp) {
        global $config;
        return date($config['format']['rss_time'], $timestamp);
    }

    /**
     *  Formats user's name, which posted message.
     *  @param  $name   username got from database
     *  @return formatted username to display on page
     */
    function formatName($name) {
        $name = parent::formatName($name);
        $name = $this->escape($name);
        return $name;
    }

    /**
     *  Formats user's email.
     *  @param  $email  email got from database
     *  @return formatted email to display on page
     */
    function formatEmail($email) {
        $email = parent::formatEmail($email);
        $email = $this->escape($email);
        return $email;
    }

    /**
     *  Formats user's URL.
     *  @param  $url    URL got from database
     *  @return formatted URL to display on page
     */
    function formatUrl($url) {
        $url = parent::formatUrl($url);
        $url = $this->escape($url);
        return $url;
    }

    /**
     *  Formats user's message.
     *  @param  $message    string message got from database
     *  @return formatted message to display on page
     */
     function formatMessage($message) {
        $message = parent::formatMessage($message);
        $message = $this->escape($message);
        return $message;
     }

    /**
     *  Replaces HTML special characters to entities which can be
     *  included into RSS &lt;description> tag.
     */
    function escape($str) {
        return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
    }

}

?>